package com.lee.controller.system;

import java.util.Map;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lee.aspect.Log;
import com.lee.base.Result;
import com.lee.constant.BusinessType;
import com.lee.system.api.IUserService;
import com.lee.system.entity.User;
import com.lee.utils.FileUploadUtils;
import com.lee.utils.ShiroUtils;

/**
 * 个人信息业务处理
 * 
 * @author LiPengFei
 *
 */
@Controller
@RequestMapping("/system/user/profile")
public class ProfileController {

	private static final Logger log = LoggerFactory.getLogger(ProfileController.class);

	private String prefix = "system/user/profile";
	@Reference
	IUserService userService;

	@GetMapping()
	public String profile(Map<String, Object> map) {
		User user = ShiroUtils.getUser();
		if ("0".equals(user.getSex())) {
			user.setSex("性别：男");
		} else if ("1".equals(user.getSex())) {
			user.setSex("性别：女");
		}
		String roleGroup = userService.selectUserRoleGroup(user.getUserId());
		String postGroup = userService.selectUserPostGroup(user.getUserId());
		map.put("user", user);
		map.put("roleGroup", roleGroup);
		map.put("postGroup", postGroup);

		return prefix + "/profile";
	}

	/**
	 * 校验旧密码是否正确
	 * 
	 * @param password
	 * @return
	 */
	@GetMapping("/checkPassWord")
	@ResponseBody
	public boolean checkPassWord(String password) {
		User user = ShiroUtils.getUser();
		if (user.getPassword()
				.equals(new Md5Hash(user.getLoginName() + password + user.getSalt()).toHex().toString())) {
			return true;
		}
		return false;
	}

	/**
	 * 修改密码
	 * 
	 * @param userId
	 * @param map
	 * @return
	 */
	@GetMapping("/restPassWord/{userId}")
	public String restPassWord(@PathVariable("userId") Long userId, Map<String, Object> map) {
		User user = ShiroUtils.getUser();
		map.put("user", user);
		return prefix + "/restPwd";
	}

	/**
	 * 修改密码
	 * 
	 * @param user
	 * @return
	 */
	@PostMapping("/restPassWord")
	@ResponseBody
	public Result restPassWord(User user) {
		if (userService.restPassWord(user) > 0) {
			ShiroUtils.setUser(userService.selectUserById(user.getUserId()));
			return Result.ok();
		}
		return Result.error();
	}

	/**
	 * 修改用户信息
	 * 
	 * @param userId
	 * @param map
	 * @return
	 */
	@GetMapping("/edit/{userId}")
	public String edit(@PathVariable("userId") Long userId, Map<String, Object> map) {
		User user = userService.selectUserById(userId);
		map.put("user", user);
		return prefix + "/edit";
	}

	@PostMapping("/update")
	@ResponseBody
	public Result update(User user) {
		if (userService.updateUser(user) > 0) {
			ShiroUtils.setUser(userService.selectUserById(user.getUserId()));
			return Result.ok();
		}
		return Result.error();
	}

	/**
	 * 修改头像
	 * 
	 * @param userId
	 * @param map
	 * @return
	 */
	@GetMapping("/avatar/{userId}")
	public String avatar(@PathVariable("userId") Long userId, Map<String, Object> map) {
		User user = ShiroUtils.getUser();
		map.put("user", user);
		return prefix + "/avatar";
	}

	/**
	 * 保存头像
	 */
	@Log(title = "个人信息", action = BusinessType.SAVE)
	@PostMapping("/updateAvatar")
	@ResponseBody
	public Result updateAvatar(User user, @RequestParam("avatarfile") MultipartFile file) {
		try {
			if (!file.isEmpty()) {
				String avatar = FileUploadUtils.upload(file);
				user.setAvatar(avatar);
				if (userService.updateUser(user) > 0) {
					ShiroUtils.setUser(userService.selectUserById(user.getUserId()));
					return Result.ok();
				}
			}
			return Result.error();
		} catch (Exception e) {
			log.error("updateAvatar failed!", e);
			return Result.error(e.getMessage());
		}
	}

}
